package com.dbb.trade.entity;

import com.dbb.trade.util.StringUtils;
import org.apache.commons.lang3.builder.ReflectionToStringBuilder;
import org.apache.commons.lang3.builder.ToStringStyle;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by IntelliJ IDEA.
 * User: Administrator
 * Date: 13-1-27
 * Time: 下午9:16
 * To change this template use File | Settings | File Templates.
 */
public class FilePath {
    private String name;

    private List<FilePath> children;

    public FilePath(String path) {
        if(path.indexOf("\\") != -1) {
            this.name = path.substring(0, path.indexOf("\\"));
            if(children == null) {
                children = new ArrayList<FilePath>();
            }
            children.add(new FilePath(path.substring(path.indexOf("\\") + 1)));
        } else {
            this.name = path;
        }
    }

    public FilePath(String name, List<FilePath> children) {
        this.name = name;
        this.children = children;
    }

    public FilePath(String name, FilePath filePath) {
        this.name = name;
        if(children == null) {
            children = new ArrayList<FilePath>();
        }
        children.add(filePath);
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public List<FilePath> getChildren() {
        return children;
    }

    public void setChildren(List<FilePath> children) {
        this.children = children;
    }

    public String getTree() {
        return "";
    }
    
    public String toString() {
        return ReflectionToStringBuilder.toString(this, ToStringStyle.SHORT_PREFIX_STYLE);
    }
    
    public static  void main(String[] args) {
        FilePath fp = new FilePath("E:\\photo\\100NCD80\\DSC_0010.JPG");
        System.out.println(fp);
    }
    
    public boolean hasChildren(String name) {
        List<String> s = new ArrayList<String>();
        if(this.children == null) {
            return false;
        }
        for(FilePath n : this.children) {
            s.add(n.getName());
        }
        return s.contains(name);
    }

    public FilePath merger(FilePath filePath) {
//        System.out.println(filePath);
        FilePath fp = filePath;
        if("/".equals(this.name)) {
            filePath = new FilePath("/", fp);
        }
        List<FilePath> fps = new ArrayList<FilePath>();
        fps.add(this);
        if(this.name.equals(filePath.getName())) {
            List<FilePath> cr = new ArrayList<FilePath>();
            if(this.children != null) {
                cr.addAll(this.children);
                List<FilePath> scns = this.children;
                List<FilePath> tcns = fp.getChildren();
                for(FilePath scn : scns) {
                    for(FilePath tcn : tcns) {
                        if(scn.getName() != null) {
                            if(!this.hasChildren(tcn.getName())) {
                                cr.add(tcn);
                            }
                            scn.merger(tcn);
                        }
                    }
                }
            } else if(fp.getChildren() != null) {
                cr.addAll(fp.getChildren());
            }
            this.children = cr;
        } else {            
            fps.add(filePath);
            System.out.println(fps);
        }
        if(fps.size() > 1) {
            return new FilePath("/", fps);
        } else {
            return fps.get(0);
        }        
    }
    
    public static FilePath merger(List<FilePath> filePaths) {
        FilePath rt = null;
        for(FilePath fp : filePaths) {
            if(rt == null) {
                rt = fp;
            }
            rt = rt.merger(fp);
        }
        return rt;
    }
    
}
